using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using System.IO;
using System.Windows.Controls;
using System.ServiceModel;

namespace ContractDocumentation
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // test command line arguments: /factor:1 /in:..\..\examples\test.wsdl /in:..\..\examples\test.xsd /out:..\..\tmp
            if (args.Length == 0)
            {
                Console.WriteLine("Help:");
                Console.WriteLine("/ws - starts web service");
                Console.WriteLine("/in:file - input file (schema or WSDL), can be specified more than once");
                Console.WriteLine("/out:dir - output directory");
                Console.WriteLine("/factor:double - images scale factor");
                return;
            }

            bool startWs = false;
            var inputFiles = new List<string>();
            string outputDir = null;
            GeneratorSettings generatorSettings = new GeneratorSettings();
            generatorSettings.ScaleFactor = 1;
            foreach (string arg in args)
            {
                if (arg == "/ws")
                {
                    startWs = true;
                }
                else if (arg.StartsWith("/in:"))
                {
                    inputFiles.Add(arg.Substring("/in:".Length));
                }
                else if (arg.StartsWith("/out:"))
                {
                    outputDir = arg.Substring("/out:".Length);
                }
                else if (arg.StartsWith("/factor:"))
                {
                    generatorSettings.ScaleFactor = double.Parse(arg.Substring("/factor:".Length));
                }
                else
                {
                    Console.WriteLine("Unknown parametr: " + arg);
                    return;
                }
            }

            if (startWs)
            {
                Uri baseAddress = new Uri("http://localhost:8080/ContractDocumentationService");
                ServiceHost serviceHost = new ServiceHost(typeof(ContractDocumentationService), baseAddress);
                serviceHost.Open();
                Console.WriteLine(string.Format("Web service started at {0}.", baseAddress));
                Console.WriteLine("To stop web service press Enter.");
                Console.ReadLine();
                if (serviceHost.State != CommunicationState.Closed) serviceHost.Close();
            }
            else
            {
                if (inputFiles.Count == 0)
                {
                    Console.WriteLine("No input file(s) specified");
                    return;
                }
                if (outputDir == null)
                {
                    Console.WriteLine("No output directory specified");
                    return;
                }

                renderXsd(inputFiles, outputDir, generatorSettings);
                //try
                //{
                //    renderXsd(inputFiles, outputDir);
                //}
                //catch (Exception e)
                //{
                //    Console.WriteLine(e);
                //}
            }
        }

        static void renderXsd(IList<string> inputFiles, string outputDir, GeneratorSettings generatorSettings)
        {
            var inputSerializedFiles = new List<SerializedFile>();
            foreach (string inputFile in inputFiles)
            {
                inputSerializedFiles.Add(new SerializedFile()
                {
                    Name = Path.GetFileName(inputFile),
                    Content = File.ReadAllBytes(inputFile),
                });
            }

            var outputSerializedFiles = Visualizer.GenerateImages(inputSerializedFiles.ToArray(), generatorSettings);

            Directory.CreateDirectory(outputDir);
            foreach (SerializedFile file in outputSerializedFiles)
	        {
                File.WriteAllBytes(Path.Combine(outputDir, file.Name), file.Content);
	        }
        }
    }
}
